package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityBondtd2NewBinding
import com.haoshuang.zhouzhoubang.model.bean.BzjBean
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.SfActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.one.BsortAdapter
import com.haoshuang.zhouzhoubang.ui.vm.BondTd2NewViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible


/**
 *
 */
class BondTd2NewActivity : BaseVMActivity<BondTd2NewViewModel, ActivityBondtd2NewBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "保证金"
    }

    lateinit var adapter2: BsortAdapter
    var deposit_money = ""
    var list2 = arrayListOf<BzjBean.DepositMenu>()
    override fun initData() {
        vm.getData()
        vm.getData2()
        vm.getData1()
        //   val deposit_money=intent.getStringExtra("deposit_money")

        binding.apply {
            tvBj.clickWithTrigger {
                if (checkbox.isChecked) {
                    startPage<BondActivity55>("team_id" to team_id)
                } else {
                    toast("请勾选协议")
                }
            }
            rvList.layoutManager = GridLayoutManager(this@BondTd2NewActivity, 5)
            adapter2 = BsortAdapter(list2)
            rvList.adapter = adapter2

            tvTh.clickWithTrigger {
                startPage<BackBondActivity2>()
                finish()
            }
            ivHead.clickWithTrigger {
                startPage<SfActivity>("url" to url)
            }
            tvMx.clickWithTrigger {
                startPage<WalletDetailsActivity5>("title" to "保证金明细")
            }
            tvXy.clickWithTrigger {
                val url = "${URLConstants.baseUrl}/index/index/agreement?id=9"
                startPage<HelpDetailActivity>("title" to "师傅保证金协议", "url" to url)
            }
        }
    }

    var url = ""
    override fun startObserve() {
        vm.getData1.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {

                    deposit_money = it.deposit_money
                    binding.depositMoney.text = deposit_money
                }
            }
        }
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list2.addAll(it?.deposit_menu!!)
                adapter2.notifyDataSetChanged()
                url = it?.deposit_money_pic!!
                binding.tvWeb.text = it?.deposit_agree

            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it?.size!! > 0) {
                    var order_amount = 0.0
                    for (b in it) {
                        order_amount += b.money.toDouble()
                    }

                    binding.tvBj.visible()
                } else {
                    binding.tvBj.gone()
                }
            }
        }
    }

}